find_package(Doxygen REQUIRED)
find_package(Perl REQUIRED)

set(PYTHON_EXECUTABLE "/usr/bin/python3")
find_package(Python3 REQUIRED)

find_package(Sphinx REQUIRED)
include(FindPythonModule)
find_python_module(breathe REQUIRED)

# 定义文档生成函数 函数名：add_breathe_doc,空格后可以带参数
function(add_breathe_doc)
    set(options)

    set(oneValueArgs
            SOURCE_DIR
            BUILD_DIR
            CACHE_DIR
            HTML_DIR
            DOXY_FILE
            CONF_FILE
            TARGET_NAME
            COMMENT
    )

    set(multiValueArgs)

    cmake_parse_arguments(BREATHE_DOC
        "${options}"
        "${oneValueArgs}"
        "${multiValueArgs}"
        ${ARGN}
    )

    configure_file(
        ${BREATHE_DOC_CONF_FILE}
        ${BREATHE_DOC_BUILD_DIR}/conf.py
        @ONLY
    )

    # 将 ${BREATHE_DOC_DOXY_FILE} 拷贝到
    configure_file(
        ${BREATHE_DOC_DOXY_FILE}
        ${BREATHE_DOC_BUILD_DIR}/Doxyfile
        @ONLY
    )

    # 自定义一个指令目标
    add_custom_target(${BREATHE_DOC_TARGET_NAME}
        # 执行 doxygen Doxyfile 生成 xml
        COMMAND ${DOXYGEN_EXECUTABLE} ${BREATHE_DOC_BUILD_DIR}/Doxyfile
        # 将生成的 xml 由 breathe 桥接给 sphinx 做格式化输出
        COMMAND
            ${SPHINX_EXECUTABLE}  # sphinx-build指令
            -q  # 仅输出警告和错误信息
            -b html # 要生成的文档类型
            -c ${BREATHE_DOC_BUILD_DIR} # 使用指定目录下的conf.py文件，默认使用源路径下的conf.py
            -d ${BREATHE_DOC_CACHE_DIR} # 解析文件时的缓存路径
            ${BREATHE_DOC_SOURCE_DIR}/rst   # 源路径，从该路径中读取主文档index.rst
            ${BREATHE_DOC_HTML_DIR} # 目标路径，最终的文档生成路径
        COMMENT
            "Building ${BREATHE_DOC_TARGET_NAME} documentation with Breathe, Sphinx and Doxygen"
        VERBATIM
    )
    # 新增目标输出
    message(STATUS "Added ${BREATHE_DOC_TARGET_NAME} [Breathe+Sphinx+Doxygen] target to build documentation")
endfunction()